home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_11 / saks / queue8.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-08  |  2.0 KB  |  112 lines

  1. Listing 5 - macro definitions for instantiating a queue of T (not a
  2. wrapper)
  3.  
  4. //
  5. // queue8.h - generic queue (not a wrapper)
  6. // with an iterator class
  7. //
  8.  
  9. #include "generic.h"
  10.  
  11. #define queue(T) name2(T, _queue)
  12.  
  13. //
  14. // queuedeclare
  15. //
  16. #define queuedeclare(T) \
  17. class queue(T) \
  18.     { \
  19. private: \
  20.     struct cell \
  21.         { \
  22.         cell(const T &e, cell *p); \
  23.         cell *next; \
  24.         T element; \
  25.         }; \
  26.     cell *first, *last; \
  27. public: \
  28.     queue(T)(); \
  29.     ~queue(T)(); \
  30.     void append(const T &e); \
  31.     void clear(); \
  32.     int remove(T &e); \
  33.     class iterator; \
  34.     friend class iterator; \
  35.     class iterator \
  36.         { \
  37.     public: \
  38.         iterator(queue(T) &q); \
  39.         T *next(); \
  40.     private: \
  41.         cell *pc; \
  42.         }; \
  43.     }; \
  44. \
  45. inline queue(T)::cell::cell(const T &e, cell *p) \
  46.     : element(e), next(p) \
  47.     { \
  48.     } \
  49. \
  50. inline queue(T)::iterator::iterator(queue(T) &q) \
  51.     : pc(q.first) \
  52.     { \
  53.     } \
  54. \
  55. inline queue(T)::queue(T)() : first(0), last(0) \
  56.     { \
  57.     } \
  58. \
  59. inline queue(T)::~queue(T)() \
  60.     { \
  61.     clear(); \
  62.     }
  63.  
  64. //
  65. // queueimplement
  66. //
  67. #define queueimplement(T) \
  68. void queue(T)::append(const T &e) \
  69.     { \
  70.     cell *p = new cell(e, 0); \
  71.     if (first == 0) \
  72.         first = p; \
  73.     else \
  74.         last->next = p; \
  75.     last = p; \
  76.     } \
  77. \
  78. T *queue(T)::iterator::next() \
  79.     { \
  80.     T *pt = 0; \
  81.     if (pc != 0) \
  82.         { \
  83.         pt = &pc->element; \
  84.         pc = pc->next; \
  85.         } \
  86.     return pt; \
  87.     } \
  88. \
  89. void queue(T)::clear() \
  90.     { \
  91.     cell *p; \
  92.     while (first != 0) \
  93.         { \
  94.         p = first; \
  95.         first = first->next; \
  96.         delete p; \
  97.         } \
  98.     } \
  99. \
  100. int queue(T)::remove(T &e) \
  101.     { \
  102.     if (first == 0) \
  103.         return 0; \
  104.     cell *p = first; \
  105.     if ((first = first->next) == 0) \
  106.         last = 0; \
  107.     e = p->element; \
  108.     delete p; \
  109.     return 1; \
  110.     }
  111.  
  112.